package de.dis2011.data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class productgroup extends productfamily
{
    private int productgroupid = -1;
//    private int productfamilyid = -1;
    private String productgroupname;

    public int getProductgroupid()
    {
        return productgroupid;
    }

    public void setProductgroupid(int productgroupid)
    {
        this.productgroupid = productgroupid;
    }

//    public int getProductfamilyid()
//    {
//        return productfamilyid;
//    }
//
//    public void setProductfamilyid(int productfamilyid)
//    {
//        this.productfamilyid = productfamilyid;
//    }

    public String getProductgroupname()
    {
        return productgroupname;
    }

    public void setProductgroupname(String productgroupname)
    {
        this.productgroupname = productgroupname;
    }

    /**
     * Lädt eine Information aus der Datenbank
     * 
     * @param id
     * 
     * @return Informations-Instanz
     */
    public static productgroup load(int id)
    {
        try
        {
            // Hole Verbindung
            Connection con = DB2ConnectionManager.getInstance().getConnection();

            // Erzeuge Anfrage
            String selectSQL = "SELECT * FROM productgroupid WHERE productgroupid = ?";
            PreparedStatement pstmt = con.prepareStatement(selectSQL);
            pstmt.setInt(1, id);

            // Führe Anfrage aus
            ResultSet rs = pstmt.executeQuery();
            if (rs.next())
            {
                productgroup ts = new productgroup();
                ts.setProductgroupid(rs.getInt("productgroupid"));
                ts.setProductfamilyid(rs.getInt("productfamilyid"));
                ts.setProductgroupname(rs.getString("name"));
                System.out.println("ID: " + ts.getProductgroupid() + "ID: " + ts.getProductfamilyid() + " Name: " + ts.getProductgroupname());
            }
            rs.close();
            pstmt.close();

        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return null;
    }

    public static productgroup loadAll()
    {
        try
        {
            // Hole Verbindung
            Connection con = DB2ConnectionManager.getInstance().getConnection();

            // Erzeuge Anfrage
            String selectSQL = "SELECT * FROM productgroupid";
            PreparedStatement pstmt = con.prepareStatement(selectSQL);

            // Führe Anfrage aus
            ResultSet rs = pstmt.executeQuery();
            while (rs.next())
            {
                productgroup ts = new productgroup();
                ts.setProductgroupid(rs.getInt("productgroupid"));
                ts.setProductfamilyid(rs.getInt("productfamilyid"));
                ts.setProductgroupname(rs.getString("name"));
                System.out.println("ID: " + ts.getProductgroupid() + "ID: " + ts.getProductfamilyid() + " Name: " + ts.getProductgroupname());
            }
            rs.close();
            pstmt.close();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return null;
    }

}
